package com.example.springbott.util;

import com.example.springbott.bean.BaseRespVo;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.silic.WXAuthRegisterBo;
import com.example.springbott.config.MarketToken;
import com.example.springbott.wx_bean_heheer.Dateinfo;
import com.example.springbott.wx_bean_heheer.LoginBO;
import com.example.springbott.wx_bean_heheer.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 登录工具类
 * @author silic xia
 * @since 2022/11/23 10:19
 */

public class WXAuthLoginUtil {

    public static BaseRespVo AuthLogin(WXAuthRegisterBo wxAuthRegisterBo){
        String username = wxAuthRegisterBo.getUsername();
        String password = wxAuthRegisterBo.getPassword();
        Subject subject = SecurityUtils.getSubject();
        MarketToken marketToken = new MarketToken(username, password, Count.TYPE_WX);
        Dateinfo dateinfo = new Dateinfo();
        UserInfo userInfo = new UserInfo();
        try {
            subject.login(marketToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return BaseRespVo.unAuthc();
        }
        if (subject.isAuthenticated()){
            MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            dateinfo.setToken(subject.getSession().getId().toString());
            userInfo.setNickName(principal.getNickname());
            userInfo.setAvatarUrl(principal.getAvatar());
            dateinfo.setUserInfo(userInfo);
            return BaseRespVo.ok(dateinfo);

        }

        return BaseRespVo.ok(dateinfo);
    }
}

